package com.wps.wdztv6.service.impl;

import cn.hutool.core.net.url.UrlQuery;
import com.wps.wdztv6.api.edit.WdztEditApi;
import com.wps.wdztv6.api.preview.WdztPreviewApi;
import com.wps.wdztv6.api.response.*;
import com.wps.wdztv6.common.R;
import com.wps.wdztv6.common.ResultEnum;
import com.wps.wdztv6.config.WdztAppInfoConfig;
import com.wps.wdztv6.entity.ThirdFile;
import com.wps.wdztv6.service.FileService;
import com.wps.wdztv6.service.WdztService;
import com.wps.wdztv6.vo.WdztViewInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

@Log4j2
@Service
public class WdztServiceImpl implements WdztService {

    @Autowired
    FileService fileService;

    @Override
    public R<String> getEditUrl(String fileId) {
        R<ThirdFile> result = fileService.getLatestFileByFileId(fileId);
        if (result.getResult() != 0){
            return R.failed(result.getMessage());
        }
        R<String> fileTypeResult = fileService.getFileType(result.getData().getName());
        if (fileTypeResult.getResult() != ResultEnum.SUCCESS.getCode()) {
            return fileTypeResult;
        }
        UrlQuery query = new UrlQuery();
        query.add("_w_third_source","edit");
        try{
            EditLinkResponse res = WdztEditApi.getEditUrl(WdztAppInfoConfig.host,result.getData().getGroupFileId(),
                    fileTypeResult.getData(),query.build(StandardCharsets.UTF_8));
            if (res != null && res.getCode() == 200) {
                return R.success("获取在线编辑成功",res.getData().getLink());
            }
            return R.failed("获取在线编辑链接失败:"+res.getMsg());
        }catch (Exception e){
            log.error("获取在线预览出错",e);
            return R.success("获取在线预览失败");
        }
    }

    @Override
    public R<WdztViewInfo> getPreviewUrl(String fileId, String previewMode) {
        R<ThirdFile> result = fileService.getLatestFileByFileId(fileId);
        if (result.getResult() != 0) {
            return R.failed(result.getMessage());
        }
        R<String> fileTypeResult = fileService.getFileType(result.getData().getName());
        if (fileTypeResult.getResult() != ResultEnum.SUCCESS.getCode()) {
            return R.failed(fileTypeResult.getMessage());
        }
        UrlQuery query = new UrlQuery();
        query.add("type",fileTypeResult.getData());
        query.add("preview_mode",previewMode);
        try {
            PreviewResponse res = WdztPreviewApi.getPreviewUrl(WdztAppInfoConfig.host, result.getData().getGroupFileId(),
                    fileTypeResult.getData(),
                    query.build(StandardCharsets.UTF_8));
            if (res != null && res.getCode() == 200) {
                String link = res.getData().getLink().replace("&wpsPreview=1111111", "");
                WdztViewInfo wdztViewInfo = WdztViewInfo.builder()
                        .origin(link+"&wpsPreview=1110001")
                        .clean(link+"&wpsPreview=0000000")
                        .build();
                return R.success("获取预览链接成功",wdztViewInfo);
            }
            return R.failed("获取在线预览链接失败:" + res.getMsg());
        } catch (Exception e) {
            log.error("获取在线预览出错", e);
            return R.failed("获取在线预览失败");
        }
    }

    @Override
    public R<PreviewPreloadResponse> previewPreload(String fileId, Integer version,String callbackUrl){
        try {
            return R.success("请求成功",WdztPreviewApi.previewPreload(WdztAppInfoConfig.host,fileId,version,callbackUrl));
        } catch (Exception e) {
            return R.failed("请求失败:"+e.getMessage());
        }
    }

    @Override
    public R<EditCooperatorsResponse> getEditOnlineCooperators(String fileId) {
        try {
            EditCooperatorsResponse result = WdztEditApi.getLinkCooperator(WdztAppInfoConfig.host,fileId);
            return R.success("请求成功", result);
        }catch (Exception e) {
            return R.failed("请求出错:"+e.getMessage());
        }
    }

    @Override
    public R<EditForceRefreshResponse> forceFreshEditLink(String fileId) {
        try {
            EditForceRefreshResponse result = WdztEditApi.forceRefreshLink(WdztAppInfoConfig.host,fileId);
            return R.success("请求成功", result);
        }catch (Exception e) {
            return R.failed("请求出错:"+e.getMessage());
        }
    }

    @Override
    public R<EditActiveSaveResponse> activeSaveEdit(String fileId, String timeout, Boolean forceSave) {
        try {
            EditActiveSaveResponse result = WdztEditApi.activeSave(WdztAppInfoConfig.host,fileId,timeout,forceSave);
            return R.success("请求成功", result);
        }catch (Exception e) {
            return R.failed("请求出错:"+e.getMessage());
        }
    }
}
